﻿using ImperialUtilities.Exceptions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImperialUtilities.Utilities
{
    public class EventViewer
    {
        private string _logSource;
        private string _entry;

        /// <summary>
        /// Constructor de la clase
        /// </summary>
        /// <param name="logSource"></param>
        public EventViewer(string logSource)
        {
            _entry = string.Empty;
            _logSource = logSource;
            try
            {
                if (!EventLog.SourceExists(_logSource))
                    EventLog.CreateEventSource(_logSource, "Application");
            }
            catch (Exception e)
            {
                string imperialMessage = "The empire is under attack. Code: EventViewer";
                throw new ImperialException(imperialMessage, e);
            }
        }

        /// <summary>
        /// Metodo que escribe en el log del sistema
        /// </summary>
        /// <param name="methodName">nombre del metodo que activa el entry</param>
        /// <param name="message">mensaje a loggear</param>
        /// <param name="isError">tipo de entry, si no, es informacion</param>
        public void WriteEventViewerLog(string methodName, string message, bool isError)
        {
            try
            {
                _entry = "." + methodName + ": " + message;
                EventLog.WriteEntry(_logSource, _entry, isError ? EventLogEntryType.Error : EventLogEntryType.Information);
            }
            catch (Exception e)
            {
                string imperialMessage = "The empire is under attack. Code: EventViewer.WriteEventLog";
                throw new ImperialException(imperialMessage, e);
            }
        }
    }
}
